package com.git.smp.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.git.smp.entitydoc.CallInfo;
import com.git.smp.entitydoc.ReqInfo;

public class ReqUtil {
	
	 /* 新增调用 */
    public static final String TRANS_TYPE_NEW_CALL = "新增调用";
    /* 修改接口 */
    public static final String TRANS_TYPE_MODIFY_INTERFACE = "修改接口";
    /* 删除调用 */
    public static final String TRANS_TYPE_DELETE_CALL = "删除调用";

    private ReqUtil() {}

    /**
     * 如果版本个数大于2
     * @param reqInfo
     * @return
     */
    public static String getInitTestVersion(ReqInfo reqInfo) {
    	if(reqInfo.getTestVersionList().size()>1) {
    		 return reqInfo.getTestVersionList().get(0);
    	}else {
    		return null ;
    	}
    	
    }

    public static List<String> getVerifyTestVersionList(ReqInfo reqInfo) {
        if (reqInfo.getTestVersionList().size() > 2) {
            return reqInfo.getTestVersionList().subList(1, reqInfo.getTestVersionList().size() - 1);
        } else {
            return null;
        }
    }
    
    
    

    public static String getPassTestVersion(ReqInfo reqInfo) {
        return reqInfo.getTestVersionList().get(reqInfo.getTestVersionList().size() - 1);
    }



    private static Map<String, List<CallInfo>> callListToMap(ReqInfo reqInfo) {
        Map<String, List<CallInfo>> transMap = new HashMap<>();

        reqInfo.getCallInfos().forEach(callInfo -> {
            transMap.computeIfAbsent(callInfo.getTransType(), k -> new ArrayList<>());

            transMap.get(callInfo.getTransType()).add(callInfo);

        });

        return transMap;
    }

    public static String getTestOverview(ReqInfo reqInfo) {

        Map<String, List<CallInfo>> transMap = callListToMap(reqInfo);

        StringBuilder builder = new StringBuilder();

        transMap.forEach((transType, callInfos) -> {
            switch (transType) {
                case TRANS_TYPE_NEW_CALL:
                    builder.append("新增调用关系：\n");
                    callInfos.forEach(callInfo -> {
                        //builder.append("消费系统【");
                        builder.append(callInfo.getConsumerSysName());
                        builder.append("调用");
                       // builder.append("】调用服务系统【");
                        builder.append(callInfo.getServiceSysName());
                        builder.append("共");
                       // builder.append("】共");
                        builder.append(callInfo.getInterfaceList().size());
                        builder.append("个接口。\n");
                        //builder.append("个接口。\n");
                    });
                    break;
                case TRANS_TYPE_MODIFY_INTERFACE:
                    builder.append("接口修改：\n");
                    callInfos.forEach(callInfo -> {
                       // builder.append("服务系统【");
                    	builder.append("服务系统");
                        builder.append(callInfo.getServiceSysName());
                        builder.append("共");
                       // builder.append("】共");
                        builder.append(callInfo.getInterfaceList().size());
                        builder.append("个接口修改。\n");
                    });
                    break;
                case TRANS_TYPE_DELETE_CALL:
                    builder.append("删除调用关系：\n");
                    callInfos.forEach(callInfo -> {
                        builder.append("删除消费系统【");
                        builder.append(callInfo.getConsumerSysName());
                        builder.append("】调用服务系统【");
                        builder.append(callInfo.getServiceSysName());
                        builder.append("】共");
                        builder.append(callInfo.getInterfaceList().size());
                        builder.append("个接口。\n");
                    });
                    break;
            }
        });


        return builder.toString();
    }

    public static String getTestRange(ReqInfo reqInfo) {
        Map<String, List<CallInfo>> transMap = callListToMap(reqInfo);

        StringBuilder builder = new StringBuilder();

        transMap.forEach((transType, callInfos) -> {
            switch (transType) {
                case TRANS_TYPE_NEW_CALL:
                    builder.append("ESB新增调用关系：\n");
                    callInfos.forEach(callInfo -> {
                       // builder.append("消费系统【");
                        builder.append(callInfo.getConsumerSysName());
                       // builder.append("】调用服务系统【");
                        builder.append("调用");
                        builder.append(callInfo.getServiceSysName());
                       // builder.append("】共");
                        builder.append("共");
                        builder.append(callInfo.getInterfaceList().size());
                        builder.append("个接口。分别是");
                        int interfaceListSize = callInfo.getInterfaceList().size();
                        for (int i = 0; i < interfaceListSize; i++) {
                            builder.append(callInfo.getInterfaceList().get(i).getTransCode());
                            if (i == interfaceListSize - 1) {
                                builder.append("。");
                            } else {
                                builder.append("、");
                            }
                        }
                    });
                    break;
                case TRANS_TYPE_MODIFY_INTERFACE:
                    builder.append("ESB接口信息修改：\n");
                    callInfos.forEach(callInfo -> {
                        builder.append("服务系统【");
                        builder.append(callInfo.getServiceSysName());
                        builder.append("】共");
                        builder.append(callInfo.getInterfaceList().size());
                        builder.append("个接口修改。分别是");
                        int interfaceListSize = callInfo.getInterfaceList().size();
                        for (int i = 0; i < interfaceListSize; i++) {
                            builder.append(callInfo.getInterfaceList().get(i).getTransCode());
                            if (i == interfaceListSize - 1) {
                                builder.append("。");
                            } else {
                                builder.append("、");
                            }
                        }
                    });
                    break;
                case TRANS_TYPE_DELETE_CALL:
                    builder.append("ESB删除调用关系：\n");
                    callInfos.forEach(callInfo -> {
                        builder.append("删除消费系统【")
                                .append(callInfo.getConsumerSysName())
                                .append("】调用服务系统【")
                                .append(callInfo.getServiceSysName())
                                .append("】共")
                                .append(callInfo.getInterfaceList().size())
                                .append("个接口。分别是");
                        int interfaceListSize = callInfo.getInterfaceList().size();
                        for (int i = 0; i < interfaceListSize; i++) {
                            builder.append(callInfo.getInterfaceList().get(i).getTransCode());
                            if (i == interfaceListSize - 1) {
                                builder.append("。");
                            } else {
                                builder.append("、");
                            }
                        }
                    });
                    break;
            }
        });

        return builder.toString();
    }

    public static String getBizDesc(ReqInfo reqInfo) {
        Map<String, List<CallInfo>> transMap = callListToMap(reqInfo);

        StringBuilder builder = new StringBuilder();

        List<CallInfo> CallInfos =  transMap.get(TRANS_TYPE_NEW_CALL);
        		if(CallInfos!= null && CallInfos.size()>0) {
        			for(int i =0 ; i<CallInfos.size();i++) {
        				 builder.append(CallInfos.get(i).getConsumerSysName())
        				 .append("发报文到ESB，ESB再将报文转发给")
 	                    .append(CallInfos.get(i).getServiceSysName());
                         if (i == CallInfos.size() - 1) {
                             builder.append("。");
                         } else {
                             builder.append("；");
                         }
        			}
        			
        			
					/*
					 * CallInfos.forEach(callInfo -> { builder.append(callInfo.getConsumerSysName())
					 * .append("发报文到ESB，ESB再将报文转发给") .append(callInfo.getServiceSysName())
					 * .append(";\n"); });
					 */
        		}
        return builder.toString();
    }

    public static String getDesignDesc(ReqInfo reqInfo) {
        Map<String, List<CallInfo>> transMap = callListToMap(reqInfo);

        StringBuilder builder = new StringBuilder();

        transMap.forEach((transType, callInfos) -> {
            switch (transType) {
                case TRANS_TYPE_NEW_CALL:
                    builder.append("ESB新增调用关系：\n");
                    callInfos.forEach(callInfo -> {
                       // builder.append("消费系统【");
                        builder.append(callInfo.getConsumerSysName());
                       // builder.append("】调用服务系统【");
                        builder.append("调用");
                        builder.append(callInfo.getServiceSysName());
                       // builder.append("】共");
                        builder.append("共");
                        builder.append(callInfo.getInterfaceList().size());
                        builder.append("个接口。分别是");
                        int interfaceListSize = callInfo.getInterfaceList().size();
                        for (int i = 0; i < interfaceListSize; i++) {
                            builder.append(callInfo.getInterfaceList().get(i).getTransCode());
                            if (i == interfaceListSize - 1) {
                                builder.append("。");
                            } else {
                                builder.append("、");
                            }
                        }
                    });
                    break;
                case TRANS_TYPE_MODIFY_INTERFACE:
                    builder.append("ESB接口信息修改：\n");
                    callInfos.forEach(callInfo -> {
                        builder.append("服务系统【");
                        builder.append(callInfo.getServiceSysName());
                        builder.append("】共");
                        builder.append(callInfo.getInterfaceList().size());
                        builder.append("个接口修改。分别是");
                        int interfaceListSize = callInfo.getInterfaceList().size();
                        for (int i = 0; i < interfaceListSize; i++) {
                            builder.append(callInfo.getInterfaceList().get(i).getTransCode());
                            if (i == interfaceListSize - 1) {
                                builder.append("。");
                            } else {
                                builder.append("、");
                            }
                        }
                    });
                    break;
                case TRANS_TYPE_DELETE_CALL:
                    builder.append("ESB删除调用关系：\n");
                    callInfos.forEach(callInfo -> {
                        builder.append("删除消费系统【")
                                .append(callInfo.getConsumerSysName())
                                .append("】调用服务系统【")
                                .append(callInfo.getServiceSysName())
                                .append("】共")
                                .append(callInfo.getInterfaceList().size())
                                .append("个接口。分别是");
                        int interfaceListSize = callInfo.getInterfaceList().size();
                        for (int i = 0; i < interfaceListSize; i++) {
                            builder.append(callInfo.getInterfaceList().get(i).getTransCode());
                            if (i == interfaceListSize - 1) {
                                builder.append("。");
                            } else {
                                builder.append("、");
                            }
                        }
                    });
                    break;
            }
        });

        return builder.toString();
    }


}
